package com.bt.lms.dao.servise.impl;

import com.bt.lms.entity.ClassInfo;
import com.bt.lms.dao.servise.ClassInfoDao;
import com.bt.lms.utils.ConnectUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

/**
 * 班级信息Dao实现
 *
 * @author 李鹏
 */
public class ClassInfoDaoImpl implements ClassInfoDao {

    @Override
    public int insert(String sql, List<ClassInfo> classInfos) {

        try {
            //关于表（class_info）
            //操作数据库
            //班级信息表
            for (int i = 0; i < classInfos.size(); i++) {
                ClassInfo classInfo = classInfos.get(i);
                String classID = classInfo.getClassID();
                String className = classInfo.getName();
                Integer stuNo = classInfo.getStuNo();
                Long divideClassRecordId = classInfo.getDivideClassRecordId();
                //jdbc,执行sql语句,知道班级信息的id
                //jdbc五步
                Connection connection = ConnectUtils.connect();
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, classID);
                ps.setString(2, className);
                ps.setInt(3, stuNo);
                ps.setLong(4, divideClassRecordId);

                int j = ps.executeUpdate();
                //5.关资源
                ps.close();
                connection.close();
            }

        } catch (Exception e) {

        }
        return 0;
    }

    @Override
    public int insertClassInfo(String sql, List<ClassInfo> classInfos, Long enrollmentPlanId) throws Exception {

        //分班信息表
        Integer classNumber = classInfos.size();
        //jdbc,执行sql语句,知道班级信息的id
        //jdbc五步
        Connection connection = ConnectUtils.connect();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, classNumber);
        ps.setLong(2, enrollmentPlanId);

        //执行
        int j = ps.executeUpdate();
        //5.关资源
        ps.close();
        connection.close();

        return j;

    }

    @Override
    public int insertHandClassInfo(String sql, List<ClassInfo> classInfos) throws Exception {

        try {
            //关于表（class_info）
            //操作数据库
            //班级信息表
            for (int i = 0; i < classInfos.size(); i++) {
                ClassInfo classInfo = classInfos.get(i);
                String classID = classInfo.getClassID();
                String className = classInfo.getName();
                Integer stuNo = classInfo.getStuNo();
                Long divideClassRecordId = classInfo.getDivideClassRecordId();
                //jdbc,执行sql语句,知道班级信息的id
                //jdbc五步
                Connection connection = ConnectUtils.connect();
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setString(1, classID);
                ps.setString(2, className);
                ps.setInt(3, stuNo);
                ps.setLong(4, divideClassRecordId);

                int j = ps.executeUpdate();
                //5.关资源
                ps.close();
                connection.close();

            }
        } catch (Exception e) {

        }
        return 0;
    }

    @Override
    public int insertHandDivideInfo(String sql, List<ClassInfo> classInfos, Long enrollmentPlanId) throws Exception {

        //分班信息表
        Integer classNumber = classInfos.size();
        //jdbc,执行sql语句,知道班级信息的id
        //jdbc五步
        Connection connection = ConnectUtils.connect();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1, classNumber);
        ps.setLong(2, enrollmentPlanId);

        //执行
        int j = ps.executeUpdate();
        //5.关资源
        ps.close();
        connection.close();

        return j;
    }

    @Override
    public List<ClassInfo> previewResult(String sql, List<ClassInfo> classInfos, Long enrollmentPlanId) throws Exception {

        //循环取值
        List<ClassInfo> list = new ArrayList<>();
        ClassInfo classInfoTwo = new ClassInfo();
        for (ClassInfo classInfo : classInfos) {
            classInfoTwo.setId(classInfo.getId());
            classInfoTwo.setName(classInfo.getName());
            classInfoTwo.setStuNo(classInfo.getStuNo());
            list.add(classInfoTwo);
        }
        return list;
    }
}
